<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm" :inline="true" :model="queryForm">
        <el-form-item :label="sysBusinessReportTemplate.reportName" prop="name">
          <el-input
            v-model="queryForm.name"
            :placeholder="sysBusinessReportTemplate.reportName"
          />
        </el-form-item>

        <el-form-item
          :label="sysBusinessReportTemplate.applicationType"
          prop="tpye"
        >
          <el-input
            v-model="queryForm.tpye"
            :placeholder="sysBusinessReportTemplate.applicationType"
          />
        </el-form-item>
        <!-- <el-form-item label="应用范围" prop="scope">
          <el-input v-model="queryForm.scope" placeholder="应用范围" />
        </el-form-item>
       -->

        <el-form-item>
          <el-button type="primary" @click="onQuery">{{
            operateType.query
          }}</el-button>
          <el-button @click="cancelForm">{{ operateType.clear }}</el-button>
        </el-form-item>
      </el-form>

      <div class="base-content-operate">
        <el-button icon="el-icon-plus" type="primary" @click="onNew">{{
          operateType.add
        }}</el-button>
      </div>

      <el-table
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
      >
        <el-table-column
          align="center"
          :label="sysBusinessReportTemplate.serialNumber"
          width="50"
        >
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessReportTemplate.reportName">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessReportTemplate.briefIntroduction">
          <template slot-scope="scope">
            {{ scope.row.introduction }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessReportTemplate.applicationType">
          <template slot-scope="scope">
            {{ scope.row.tpyeName }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessReportTemplate.applicationScope">
          <template slot-scope="scope">
            <!-- <el-select v-model="scope.row.scope" disabled>
              <el-option
                v-for="(item, index) in reportTypeList"
                :key="index"
                :label="item.name"
                :value="item.dictCode"
              />
            </el-select> -->
            {{ scope.row.scopeName }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessReportTemplate.attachmentTemplate">
          <template slot-scope="scope">
            <a
              style="color: blue; cursor: pointer"
              @click="previewReport(scope.row)"
            >
              {{ scope.row.attachmentName }}</a
            >
          </template>
        </el-table-column>

        <el-table-column :label="sysBusinessReportTemplate.updatePerson">
          <template slot-scope="scope">
            {{ scope.row.updaterName }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessReportTemplate.updateTime">
          <template slot-scope="scope">
            {{ scope.row.updateTime }}
          </template>
        </el-table-column>
        <el-table-column :label="operateType.action">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)"
                >{{ operateType.redact }}</el-link
              >
              <el-link
                :disabled="scope.row.attachmentName === ''"
                type="primary"
                @click="previewReport(scope.row)"
                >{{ operateType.preview }}</el-link
              >
              <el-link
                :disabled="scope.row.attachmentName === ''"
                type="primary"
                @click="downLoadFile(scope.row)"
                >{{ operateType.download }}</el-link
              >
              <el-link
                v-if="!scope.row.reportTypeEnum != 0"
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >{{ operateType.delete }}</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :current-page="pagination.pageIndex"
        :page-count="pagination.total"
        @changePage="changePage"
        @sizeChange="sizeChange"
      />
    </div>

    <SysBusinessReportTemplateEdit
      v-if="dialogFormVisible"
      ref="SysBusinessReportTemplateEdit"
      :edit-id.sync="editId"
      :dialog-form-visible.sync="dialogFormVisible"
    />
    <BasePreviewFile
      v-if="previewFileShow"
      :preview-file-show.sync="previewFileShow"
      :preview-file="filepreview"
    />
  </div>
</template>

<script>
import {
  deleteRecord,
  getList,
} from "../../../api/SysBusiness/SysBusinessReportTemplate";
import SysBusinessReportTemplateEdit from "./SysBusinessReportTemplateEdit.vue";
import { GetStaticFileInfo } from "@/api/Base";
export default {
  name: "SysBusinessReportTemplateList",
  components: {
    SysBusinessReportTemplateEdit,
  },
  data() {
    return {
      loading: true,
      list: [],
      queryForm: {
        name: "",
        introduction: "",
        tpye: "",
        scope: "",
        attachmentName: "",
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
      previewFileShow: false,
      filepreview: {
        fileName: "",
        url: "",
        storageFileName: "",
      },
      editId: "",
      reportTypeList: [],
    };
  },
  computed: {
    operateType() {
      return this.$t("ModuleSysBusiness.OperateType") || {};
    },
    sysBusinessReportTemplate() {
      return this.$t("SysBusinessReportTemplate") || {};
    },
  },
  created() {
    this.getList();
  },
  methods: {
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.pagination.pageIndex = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },

    onNew() {
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(rowData) {
      this.editId = rowData.id;

      this.dialogFormVisible = true;
    },
    // 预览报告
    previewReport(row) {
      if (row.attachmentTemplate !== null && row.attachmentTemplate !== "") {
        this.filepreview.fileName = row.attachmentName;
        this.filepreview.url = row.attachmentTemplate;
        this.filepreview.storageFileName = row.attachmentName;
        this.previewFileShow = true;
      }
    },
    // 下载
    downLoadFile(row) {
      GetStaticFileInfo(row.attachmentTemplate, row.attachmentName);
    },
    onQuery() {
      this.getList();
    },
  },
};
</script>
